﻿#include "clientmanager.h"
#include "tcpmanager.h"
#include "transfersocket.h"
#include "Sington.h"

ClientManager::ClientManager()
{
    connect(TcpManager::instance(), &TcpManager::newConnection, this, &ClientManager::onNewConnection);
    connect(TcpManager::instance(),&TcpManager::DisConnection,this,&ClientManager::onSwitchClosedClicked);
}

ClientManager *ClientManager::instance()
{
    return Sington<ClientManager>::getInstance();
}

void ClientManager::onSwitchChangedClicked()
{
     TcpManager *tcpManager = TcpManager::instance();
     bool islistening = tcpManager->listen();
     if(islistening)
     {
         qDebug() << "isListening";
     }
}
/**
 * @brief ClientManager::onSwitchClosedClicked
 * 断开连接
 */
void ClientManager::onSwitchClosedClicked()
{
    TcpManager *tcpManager = TcpManager::instance();
    //Q_UNUSED(a);
    bool islistening = tcpManager->listen();
    if(islistening&&tcpManager)
    {
        //tcpManager->disconnectFromHost(peerAddress,peerPort);
        tcpManager->onSocketDisconnect();
        tcpManager->disconnect();
        tcpManager->close();
    }

    //    m_manager->onSocketDisconnect();
}

void ClientManager::onNewConnection(QSharedPointer<TransferSocket> socket)
{
    QString peerAdd = socket->peerAddress().toString();
    qint16 theport = socket->peerPort();
    QString temp = QString("[%1:%2]:成功连接").arg(peerAdd).arg(theport);
    emit hasnewconnect(temp);
}
